from fastapi import FastAPI, Depends
from configuration.config import DSM5Settings, ServerSettings

from myDSM.security.secure import UsernameAuthBackend
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware

from myDSM.api import disorders_api

middleware = [Middleware(AuthenticationMiddleware,
                         backend=UsernameAuthBackend("openminds"))]


dsm_app = FastAPI(middleware=middleware)
dsm_app.include_router(disorders_api.router)


def build_config():
    return DSM5Settings()


def fetch_config():
    return ServerSettings()


@dsm_app.get('/index')
def index_dsm(config: DSM5Settings = Depends(build_config),
              fconfig: ServerSettings = Depends(fetch_config)):
    return {
        'project_name': config.application,
        'webmaster': config.webmaster,
        'created': config.created,
        'development_server': fconfig.development_server,
        'dev_port': fconfig.dev_port
        }
